<?php
/**
 * (exporter) 成品供应商-导出
 */
namespace App\Exporter\Basics;

use App\Models\Basics\Supplier;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class SupplierExporter extends ExcelExporter implements WithMapping,WithHeadings
{

    protected $fileName = '成品供应商.csv';

    public function headings(): array
    {
        $head1 = ['编码','名称','简称','结算方式','主营产品','详细地址','运费类型','税点','审核','启用'];
        $head2 = ['开户行','开户人','银行卡号','身份证号'];
        $head3 = ['联系人','手机号','职务','座机号','QQ','微信','邮箱'];
        return array_merge($head1,$head2,$head3);
    }

    /**
     * 模型数据
     * @param mixed $model
     * @return array
     */
    public function map($model) : array
    {
        $count = 0;
        $data = [];

        //供应商
        $data1 = [
            $model->code ?? '',
            $model->name ?? '',
            $model->short_name ?? '',
            $model->settlement_method->settlement_method ?? '',
            $model->main_product ?? '',
            $model->address ?? '',
            Supplier::$_shipFeeType[$model->ship_fee_type] ?? '',
            $model->tax ?? '',
            Supplier::$_auditStatus[$model->audit_status] ?? '',
            Supplier::$_status[$model->status] ?? '',
        ];
        $data[$count] = $data1;

        //供应商账号
        $data2 = [
            $model->supplier_account->bank ?? '',
            $model->supplier_account->accounter ?? '',
            $model->supplier_account->bank_number ?? '',
            $model->supplier_account->identity_number ?? '',
        ];
        $data[$count] = array_merge($data1,$data2);

        //供应商联系人
        foreach($model->supplier_contacts as $key => $item){
            if ($key == 0) {
                //单个(单行)
                $data3 = [
                    $item->contact ?? '',
                    $item->cellphone ?? '',
                    $item->position ?? '',
                    $item->telephone ?? '',
                    $item->qq ?? '',
                    $item->we_chat ?? '',
                    $item->email ?? '',
                ];
                $data[$count] = array_merge($data1,$data2,$data3);
            }else{
                //多个联系人(多行显示)
                $count++; //追加1行
                $data3 = [
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    '',
                    $item->contact ?? '',
                    $item->cellphone ?? '',
                    $item->position ?? '',
                    $item->telephone ?? '',
                    $item->qq ?? '',
                    $item->we_chat ?? '',
                    $item->email ?? '',
                ];
                $data[$count] = $data3;
            }
        }

        return $data;
    }

}
